import { _decorator, Component, instantiate, Vec3, Node, tween, randomRange } from 'cc';

const { ccclass, property } = _decorator;

@ccclass('DiceController') // 用 @ccclass 声明类为 Cocos 组件
export class DiceController extends Component {
    // 通过 @property 让编辑器可以配置属性
    @property(Node) dice1!: Node;
    @property(Node) dice2!: Node;

    private animation : boolean = false;
    private step : number = 0.1;
    private modelDice: Vec3[] = [
        new Vec3(-90, 0, 0),
        new Vec3(180, 0, 0),
        new Vec3(0, 0, -90),
        new Vec3(0, 0, 90),
        new Vec3(0, 0, 0),
        new Vec3(90, 0, 0),
    ];

    private diceIndex1 = 0;
    private diceIndex2 = 0;

    onLoad() {

    }

    start(){
        // this.rollNumber();
        // this.scheduleOnce(this.rollNumber.bind(this), 0.3);


    }

    randomNumber(){
        const index = Math.floor(Math.random() * 6) + 1;
        const index2 = Math.floor(Math.random() * 6) + 1;
        this.animationNumber(index, index2);
    }

    animationNumber(n1:number, n2:number){
        // console.log("animationNumber", n1, n2);

        this.diceIndex1 = n1 - 1;
        this.diceIndex2 = n2 - 1;

        this.rollNumber();
    }

    rollNumber(){
        this.animation = true;
        this.scheduleOnce(()=>{
                this.animation = false;
            }, 0.5
        );
        this.simulateDiceMovement(this.dice1);
        this.simulateDiceMovement(this.dice2);
    }


    simulateDiceMovement(diceNode: Node) {
        
        const duration = this.step; // 动画持续时间
        const startPosition = diceNode.getPosition();

        // 创建随机的移动范围
        // const randomTargetPosition = new Vec3(
        //     startPosition.x + randomRange(-0.05, 0.05),
        //     startPosition.y/* + randomRange(-0.5, 0.5)*/,
        //     startPosition.z + randomRange(-0.05, 0.05)
        // );

        const randomTargetPosition = new Vec3(
            randomRange(-0.02, 0.02),
            startPosition.y,
            randomRange(-0.02, 0.02)
        );


        // 创建随机的旋转角度
        const randomTargetRotation = new Vec3(
            randomRange(0, 360),
            randomRange(0, 360),
            randomRange(0, 360)
        );

        // 使用 tween 创建动画
        tween(diceNode)
            .to(duration, { position: randomTargetPosition, eulerAngles: randomTargetRotation })
            .call(() => {
                // 动画结束后递归调用
                if(this.animation)
                    this.simulateDiceMovement(diceNode);
                else
                    this.endDiceMovement(diceNode);
            })
            .start();
    }

    endDiceMovement(diceNode: Node) {
        
        const duration = this.step * 2; // 动画持续时间
        const startPosition = diceNode.getPosition();

        let randomTargetPosition;
        let randomTargetRotation;

        if(diceNode == this.dice1)
        {
            randomTargetPosition = new Vec3(
                randomRange(-0.01, -0.005),
                startPosition.y,
                randomRange(-0.01, 0.01)
            );

            randomTargetRotation = this.modelDice[this.diceIndex1];

        }
        else
        {
            randomTargetPosition = new Vec3(
                randomRange(0.005, 0.01),
                startPosition.y,
                randomRange(-0.01, 0.01)
            );

            randomTargetRotation = this.modelDice[this.diceIndex2];
        }


        // 使用 tween 创建动画
        tween(diceNode)
        .to(duration, { position: randomTargetPosition, eulerAngles: randomTargetRotation })
        .call(() => {
        })
        .start();
    }

}